package appx.craft.asd.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;
import appx.craft.asd.R;
import appx.craft.asd.uc.ApplicationTopBar;

// TODO: Auto-generated Javadoc
/**
 * The Class ChnagePasswordActivity.
 */
public class ChnagePasswordActivity extends Activity implements OnClickListener,OnTouchListener,OnFocusChangeListener{
	
	/** The tag. */
	public final String TAG = ChnagePasswordActivity.class.getCanonicalName();
	
	/** The m top bar. */
	private ApplicationTopBar mTopBar;
	
	/** The btn_chnagepwd_cancel. */
	private Button btn_chnagepwd_update,btn_chnagepwd_cancel;
	
	/** The edittext_chnagepwd_confirm. */
	private EditText edittext_chnagepwd_old,edittext_chnagepwd_new,edittext_chnagepwd_confirm;
	
	/** The layout_root_view. */
	private RelativeLayout layout_root_view;
	
	/** The edit text temp. */
	private EditText editTextTemp;
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG,"[ onCreate ] ");
		setContentView(R.layout.activity_change_password);
		mTopBar = (ApplicationTopBar)findViewById(R.id.top_bar_application);
		mTopBar.setTitle(getResources().getString(R.string.activity_changepassword));
		
		layout_root_view = (RelativeLayout)findViewById(R.id.layout_root_view);
		layout_root_view.setOnTouchListener(this);
		
		btn_chnagepwd_update = (Button)findViewById(R.id.btn_chnagepwd_update);
		btn_chnagepwd_update.setOnClickListener(this);
		btn_chnagepwd_cancel = (Button)findViewById(R.id.btn_chnagepwd_cancel);
		btn_chnagepwd_cancel.setOnClickListener(this);
		
		edittext_chnagepwd_old = (EditText)findViewById(R.id.edittext_chnagepwd_old);
		edittext_chnagepwd_old.setOnFocusChangeListener(this);
		edittext_chnagepwd_new = (EditText)findViewById(R.id.edittext_chnagepwd_new);
		edittext_chnagepwd_new.setOnFocusChangeListener(this);
		
		edittext_chnagepwd_confirm = (EditText)findViewById(R.id.edittext_chnagepwd_confirm);
		edittext_chnagepwd_confirm.setOnFocusChangeListener(this);
		edittext_chnagepwd_confirm.setOnFocusChangeListener(new OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if(hasFocus){
					
					if(edittext_chnagepwd_new.getText().toString().trim().length()>0){
						
					}else{
						showAlertDialog("Alert : Password","Please enter new password");
						edittext_chnagepwd_new.requestFocus();
					}
				}
			}
		});
				
	}
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onStart()
	 */
	@Override
	protected void onStart() {
		super.onStart();
		Log.d(TAG,"[ onStart ] ");
	}
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onStop()
	 */
	@Override
	protected void onStop() {
		super.onStop();
		Log.d(TAG,"[ onStop ] ");
	}
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onPause()
	 */
	@Override
	protected void onPause() {
		super.onPause();
		Log.d(TAG,"[ onPause ] ");
		hideKeyBoard();
		
	}
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onResume()
	 */
	@Override
	protected void onResume() {
		super.onResume();
		Log.d(TAG,"[ onResume ] ");
	}
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onDestroy()
	 */
	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.d(TAG,"[ onDestroy ] ");
	}
	
	/* (non-Javadoc)
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		int buttonID = v.getId();
		switch (buttonID) {
		case R.id.btn_chnagepwd_update:
			String newPassword = edittext_chnagepwd_new.getText().toString().trim();
			String s = edittext_chnagepwd_confirm.getText().toString().trim();
			if(!newPassword.equals("") || !s.equals("")){
				
				if(!newPassword.equals(s)){
					showAlertDialog("Alert : Password","Password not match");
				}
				
			}else{
				showAlertDialog("Alert : Password","Password field should not blank");
			}
			Toast.makeText(getApplicationContext(),"Update Pressed",Toast.LENGTH_LONG).show();
			
			break;
		case R.id.btn_chnagepwd_cancel:
			Toast.makeText(getApplicationContext(),"Cancel Pressed",Toast.LENGTH_LONG).show();
			break;
		default:
			break;
		}
	}
	
	/**
	 * Show alert dialog.
	 *
	 * @param title the title
	 * @param message the message
	 */
	private void showAlertDialog(String title,String message){
		
		AlertDialog.Builder alertDialog = new AlertDialog.Builder(ChnagePasswordActivity.this);
		// Setting Dialog Title
		alertDialog.setTitle(title);

		// Setting Dialog Message
		alertDialog.setMessage(message);

		// Setting Icon to Dialog
		alertDialog.setIcon(android.R.drawable.ic_dialog_alert);

	    alertDialog.setPositiveButton("OK",new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				
			}
		});

		// Showing Alert Message
		alertDialog.show();
	}

	/* (non-Javadoc)
	 * @see android.view.View.OnTouchListener#onTouch(android.view.View, android.view.MotionEvent)
	 */
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
		return false;
	}

	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		View mView = v;
		if(mView instanceof EditText){
			editTextTemp = (EditText) mView;
			hideKeyBoard((EditText)mView);
		}
	}
	
	public void hideKeyBoard(EditText editTextTemp){
		InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
		if(editTextTemp != null){
			imm.hideSoftInputFromWindow(editTextTemp.getWindowToken(), 0);
		}
	}
	public void hideKeyBoard(){
		InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(editTextTemp.getWindowToken(), 0);
	}
	
}
